@tool
class_name Skill_Control_While
extends SkillControl

## While 只要
## 只要条件还满足就会一直执行
#region 属性
@export_multiline var expression:String
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.release(args)
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	if not _check_condition(cur_blackboard):
		return GameEnum.SkillReleaseStatus.Failure
	return GameEnum.SkillReleaseStatus.Success
## 判断技能是否释放完成（返回true，代表可以直接结束，也可以添加一些处理逻辑，实现强制结束（例如持续施法消耗不够））
func check_is_completed() -> bool:
	var res = super.check_is_completed()
	if res != true:
		return res
	return not _check_condition(cur_blackboard)
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Success)
	else:
		reset_cool()
		release(release_args)
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Interrupted)
#endregion
#region 公共方法
#endregion
#region 私有方法
func _check_condition(blackboard:Blackboard) -> bool:
	var ex:Expression = Expression.new()
	var error = ex.parse(expression,blackboard.keys())
	if error != OK:
		return false
	var result = ex.execute(blackboard.values(),self)
	if not ex.has_execute_failed():
		return result
	return false
#endregion
#region 生命周期函数
func _process(delta: float) -> void:
	super._process(delta)
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Success)
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	if expression.is_empty():
		warnings.append("expression不能为空，且必须是一个bool表达式")
	if get_child_count() > 1:
		warnings.append("此节点最多只能有一个节点")
	return warnings
#endregion
